// Decompiled by DJ v3.10.10.93 Copyright 2007 Atanas Neshkov  Date: 2011-7-18 11:10:44
// Home Page: http://members.fortunecity.com/neshkov/dj.html  http://www.neshkov.com/dj.html - Check often for new version!
// Decompiler options: packimports(3) 
// Source File Name:   JspFileUpload.java

package com.dd.util;

import java.io.*;
import java.util.ArrayList;
import java.util.Hashtable;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;

public class JspFileUpload {

	public JspFileUpload() {
		request = null;
		uploadPath = null;
		paramHt = new Hashtable();
		updFileArr = new ArrayList();
	}

	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}

	public void setUploadPath(String path) {
		uploadPath = path;
	}

	public int process() {
		int status = 0;
		status = preCheck();
		if (status != 0)
			return status;
		try {
			String name = null;
			String value = null;
			boolean fileFlag = false;
			File tmpFile = null;
			String fName = null;
			FileOutputStream baos = null;
			BufferedOutputStream bos = null;
			paramHt = new Hashtable();
			updFileArr = new ArrayList();
			int rtnPos = 0;
			byte buffs[] = new byte[BUFSIZE * 8];
			String contentType = request.getContentType();
			int index = contentType.indexOf("boundary=");
			String boundary = (new StringBuilder("--")).append(
					contentType.substring(index + 9)).toString();
			String endBoundary = (new StringBuilder(String.valueOf(boundary)))
					.append("--").toString();
			ServletInputStream sis = request.getInputStream();
			while ((rtnPos = sis.readLine(buffs, 0, buffs.length)) != -1) {
				String strBuff = new String(buffs, 0, rtnPos);
				if (strBuff.startsWith(boundary)) {
					if (name != null && name.trim().length() > 0)
						if (fileFlag) {
							bos.flush();
							baos.close();
							bos.close();
							baos = null;
							bos = null;
							updFileArr.add(fName);
						} else {
							Object obj = paramHt.get(name);
							ArrayList al = new ArrayList();
							if (obj != null)
								al = (ArrayList) obj;
							al.add(value);
							paramHt.put(name, al);
						}
					name = new String();
					value = new String();
					fileFlag = false;
					fName = new String();
					rtnPos = sis.readLine(buffs, 0, buffs.length);
					if (rtnPos != -1) {
						strBuff = new String(buffs, 0, rtnPos);
						if (strBuff.toLowerCase().startsWith(
								"content-disposition: form-data; ")) {
							int nIndex = strBuff.toLowerCase().indexOf(
									"name=\"");
							int nLastIndex = strBuff.toLowerCase().indexOf(
									"\"", nIndex + 6);
							name = strBuff.substring(nIndex + 6, nLastIndex);
						}
						int fIndex = strBuff.toLowerCase().indexOf(
								"filename=\"");
						if (fIndex != -1) {
							fileFlag = true;
							int fLastIndex = strBuff.toLowerCase().indexOf(
									"\"", fIndex + 10);
							fName = strBuff.substring(fIndex + 10, fLastIndex);
							fName = getFileName(fName);
							if (fName == null || fName.trim().length() == 0) {
								fileFlag = false;
								sis.readLine(buffs, 0, buffs.length);
								sis.readLine(buffs, 0, buffs.length);
								sis.readLine(buffs, 0, buffs.length);
							} else {
								fName = getFileNameByTime(fName);
								sis.readLine(buffs, 0, buffs.length);
								sis.readLine(buffs, 0, buffs.length);
							}
						}
					}
				} else if (strBuff.startsWith(endBoundary)) {
					if (name != null && name.trim().length() > 0)
						if (fileFlag) {
							bos.flush();
							baos.close();
							bos.close();
							baos = null;
							bos = null;
							updFileArr.add(fName);
						} else {
							Object obj = paramHt.get(name);
							ArrayList al = new ArrayList();
							if (obj != null)
								al = (ArrayList) obj;
							al.add(value);
							paramHt.put(name, al);
						}
				} else if (fileFlag) {
					if (baos == null && bos == null) {
						tmpFile = new File((new StringBuilder(String
								.valueOf(uploadPath))).append(fName).toString());
						baos = new FileOutputStream(tmpFile);
						bos = new BufferedOutputStream(baos);
					}
					bos.write(buffs, 0, rtnPos);
					baos.flush();
				} else {
					value = (new StringBuilder(String.valueOf(value))).append(
							strBuff).toString();
				}
			}
		} catch (IOException e) {
			status = 4;
		}
		return status;
	}

	private void removeLast2() {
		String[] fileArr = this.getUpdFileNames();
		String srcFile = fileArr[0];
		try {
			File file = new File(request.getRealPath("/") + "/up/" + srcFile);
			/*
			 * InputStream is = new FileInputStream(file); long length =
			 * file.length(); byte[] bytes = new byte[(int) length]; int offset
			 * = 0; int numRead = 0; while (offset < bytes.length && (numRead =
			 * is.read(bytes, offset, bytes.length - offset)) >= 0) { offset +=
			 * numRead; } bytes.
			 */
			File oldfile = new File(request.getRealPath("/") + "/up/tem.file");
			long filesize = oldfile.length();
			if (oldfile.exists()) {
				long bytesum = 0;
				int byteread = 0;
				InputStream inStream = new FileInputStream(request
						.getRealPath("/")
						+ "/up/tem.file");
				FileOutputStream fs = new FileOutputStream(request
						.getRealPath("/")
						+ "/up/" + srcFile);
				byte buffer[] = new byte[1444];
				while ((byteread = inStream.read(buffer)) != -1) {
					bytesum += byteread;
					if (bytesum >= filesize) {
						fs.write(buffer, 0, byteread - 2);
					} else {
						fs.write(buffer, 0, byteread);
					}

				}
				inStream.close();
				// oldfile.delete();
			}

		} catch (Exception e) {
			e.printStackTrace();

		}
	}

	private int preCheck() {
		int errCode = 0;
		if (request == null)
			return 1;
		if (uploadPath == null || uploadPath.trim().length() == 0)
			return 2;
		File tmpF = new File(uploadPath);
		if (!tmpF.exists())
			return 2;
		String contentType = request.getContentType();
		if (contentType.indexOf("multipart/form-data") == -1)
			return 3;
		else
			return errCode;
	}

	public String getParameter(String name) {
		String value = "";
		if (name == null || name.trim().length() == 0) {
			return value;
		} else {
			value = paramHt.get(name) != null ? (String) ((ArrayList) paramHt
					.get(name)).get(0) : "";
			return value;
		}
	}

	public String[] getParameters(String name) {
		if (name == null || name.trim().length() == 0)
			return null;
		if (paramHt.get(name) == null)
			return null;
		ArrayList al = (ArrayList) paramHt.get(name);
		String strArr[] = new String[al.size()];
		for (int i = 0; i < al.size(); i++)
			strArr[i] = (String) al.get(i);

		return strArr;
	}

	public int getUpdFileSize() {
		return updFileArr.size();
	}

	public String[] getUpdFileNames() {
		String strArr[] = new String[updFileArr.size()];
		for (int i = 0; i < updFileArr.size(); i++)
			strArr[i] = (String) updFileArr.get(i);

		return strArr;
	}

	private String getFileName(String input) {
		try {
			int fIndex = input.lastIndexOf("\\");
			if (fIndex == -1) {
				fIndex = input.lastIndexOf("/");
				if (fIndex == -1)
					return input;
			}
			input = input.substring(fIndex + 1);
			System.out.println("getFileName" + input);
			String str = new String(input.getBytes("utf-8"),"gbk");
			System.out.println(str);
			str = new String(input.getBytes("ISO-8859-1"),"gbk");
			System.out.println(str);
			str = new String(input.getBytes("ISO-8859-1"),"gb2312");
			System.out.println(str);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return input;
	}

	private String getFileNameByTime(String input) {
		return input;
	}

	public void remove() {
		boolean flag = false;
		String path = request.getRealPath("/") + "/up/";
		File file = new File(path);
		String[] tempList = file.list();
		File temp = null;
		for (int i = 0; i < tempList.length; i++) {
			if (tempList[i].indexOf("item_list") != -1) {
				continue;
			}
			temp = new File(path + tempList[i]);
			temp.delete();
		}
	}

	public int getMaxHtmlNum(String path, String filename) {
		File file = new File(path);
		filename = filename.substring(0, filename.indexOf("."));
		File files[] = file.listFiles();
		int max = 10000;
		for (int i = 0; i < files.length; i++) {
			String str = files[i].toString();
			if (str.indexOf(filename) != -1 && str.endsWith("html")) {
				int index1 = str.indexOf(path) + path.length();
				str = str.substring(index1, str.length());
				index1 = str.indexOf(".");
				str = str.substring(index1 - 5, index1);
				try {
					int tem = Integer.parseInt(str);
					if (tem > max)
						max = tem;
				} catch (Exception e) {
					// e.printStackTrace();
				}
			}
		}

		return max;
	}

	public int getNextFileNum(String path, String filename) {
		File file = new File(path);
		filename = filename.substring(0, filename.indexOf("."));
		File files[] = file.listFiles();
		int max = 10000;
		for (int i = 0; i < files.length; i++) {
			String str = files[i].toString();
			if (str.indexOf(filename) != -1 && !str.endsWith("html")) {
				int index1 = str.indexOf(path) + path.length();
				str = str.substring(index1, str.length());
				index1 = str.indexOf(".");
				str = str.substring(index1 - 5, index1);
				try {
					int tem = Integer.parseInt(str);
					if (tem > max)
						max = tem;
				} catch (Exception e) {
					// e.printStackTrace();
				}
			}
		}

		return max;
	}

	public void copy(String rootpath, String target, String filename, int max) {
		String str1 = filename.substring(0, filename.indexOf("."));
		String str2 = filename.substring(filename.indexOf(".") + 1);
		String newfilename = "";
		if (max == 0)
			newfilename = filename;
		else
			newfilename = (new StringBuilder(String.valueOf(str1))).append(max)
					.append(".").append(str2).append(".zip").toString();
		copyFile((new StringBuilder(String.valueOf(rootpath))).append("/up/")
				.append(filename).toString(), (new StringBuilder(String
				.valueOf(rootpath))).append("/").append(target).append("/")
				.append(newfilename).toString());
	}

	public void copy1(String rootpath, String target, String filename, int max) {
		// key_C_o_n_f_i_g.xml
		String str1 = filename.substring(0, filename.indexOf("."));
		String str2 = filename.substring(filename.indexOf(".") + 1);
		String newfilename = "";
		newfilename = (new StringBuilder(String.valueOf(str1))).append(max)
				.append(".").append(str2).toString();
		System.out.println("newfilename:" + newfilename);
		try {
			PrintWriter pw = new PrintWriter(new OutputStreamWriter(
					new FileOutputStream(rootpath + "/" + target
							+ "/keyConfig.xml.zip")), true);
			String str = readFileByLines(rootpath + "/" + target
					+ "/key_C_o_n_f_i_g.xml");
			str = str.replaceAll("[r][a][t][e][=][\"]\\d{2}[\"]", "");
			str = str.replaceAll("[r][a][t][e][=][\"]\\d{3}[\"]", "");
			str = str.replaceAll("[r][a][t][e][=][\"]\\d{4}[\"]", "");
			str = str.replaceAll("[r][a][t][e][=][\"]\\d{5}[\"]", "");
			str = str.replaceAll("[r][a][t][e][2][=][\"]\\d{2}[\"]", "");
			str = str.replaceAll("[r][a][t][e][2][=][\"]\\d{3}[\"]", "");
			str = str.replaceAll("[r][a][t][e][2][=][\"]\\d{4}[\"]", "");
			str = str.replaceAll("[r][a][t][e][2][=][\"]\\d{5}[\"]", "");
			pw.println(str);
			pw.flush();
			pw.close();
		} catch (Exception e) {
			e.printStackTrace();
		}

		copyFile((new StringBuilder(String.valueOf(rootpath))).append("/xml/")
				.append(filename).toString(), (new StringBuilder(String
				.valueOf(rootpath))).append("/").append(target).append("/")
				.append(newfilename).toString());
	}

	public void copy2(String rootpath, String target, String filename, int max) {
		// item_r_a_t_e_s_e_r_v_e_r.xml
		String str1 = filename.substring(0, filename.indexOf("."));
		String str2 = filename.substring(filename.indexOf(".") + 1);
		String newfilename = "";
		newfilename = (new StringBuilder(String.valueOf(str1))).append(max)
				.append(".").append(str2).toString();
		System.out.println("newfilename:" + newfilename);
		try {
			PrintWriter pw = new PrintWriter(new OutputStreamWriter(
					new FileOutputStream(rootpath + "/" + target
							+ "/item_list.xml.zip")), true);
			String str = readFileByLines(rootpath + "/" + target
					+ "/item_r_a_t_e_s_e_r_v_e_r.xml");
			str = str.replaceAll("[r][a][t][e][=][\"]\\d{2}[\"]", "");
			str = str.replaceAll("[r][a][t][e][=][\"]\\d{3}[\"]", "");
			str = str.replaceAll("[r][a][t][e][=][\"]\\d{4}[\"]", "");
			str = str.replaceAll("[r][a][t][e][=][\"]\\d{5}[\"]", "");
			str = str.replaceAll("[r][a][t][e][2][=][\"]\\d{2}[\"]", "");
			str = str.replaceAll("[r][a][t][e][2][=][\"]\\d{3}[\"]", "");
			str = str.replaceAll("[r][a][t][e][2][=][\"]\\d{4}[\"]", "");
			str = str.replaceAll("[r][a][t][e][2][=][\"]\\d{5}[\"]", "");
			str = str.replaceAll("[r][a][t][e][3][=][\"]\\d{2}[\"]", "");
			str = str.replaceAll("[r][a][t][e][3][=][\"]\\d{3}[\"]", "");
			str = str.replaceAll("[r][a][t][e][3][=][\"]\\d{4}[\"]", "");
			str = str.replaceAll("[r][a][t][e][3][=][\"]\\d{5}[\"]", "");
			str = str.replaceAll("[r][a][t][e][1][1][=][\"]\\d{2}[\"]", "");
			str = str.replaceAll("[r][a][t][e][1][1][=][\"]\\d{3}[\"]", "");
			str = str.replaceAll("[r][a][t][e][1][1][=][\"]\\d{4}[\"]", "");
			str = str.replaceAll("[r][a][t][e][1][1][=][\"]\\d{5}[\"]", "");
			str = str.replaceAll("[r][a][t][e][1][2][=][\"]\\d{2}[\"]", "");
			str = str.replaceAll("[r][a][t][e][1][2][=][\"]\\d{3}[\"]", "");
			str = str.replaceAll("[r][a][t][e][1][2][=][\"]\\d{4}[\"]", "");
			str = str.replaceAll("[r][a][t][e][1][2][=][\"]\\d{5}[\"]", "");

			str = str.replaceAll("[r][a][t][e][1][3][=][\"]\\d{2}[\"]", "");
			str = str.replaceAll("[r][a][t][e][1][3][=][\"]\\d{3}[\"]", "");
			str = str.replaceAll("[r][a][t][e][1][3][=][\"]\\d{4}[\"]", "");
			str = str.replaceAll("[r][a][t][e][1][3][=][\"]\\d{5}[\"]", "");

			str = str.replaceAll("[r][a][t][e][1][4][=][\"]\\d{2}[\"]", "");
			str = str.replaceAll("[r][a][t][e][1][4][=][\"]\\d{3}[\"]", "");
			str = str.replaceAll("[r][a][t][e][1][4][=][\"]\\d{4}[\"]", "");
			str = str.replaceAll("[r][a][t][e][1][4][=][\"]\\d{5}[\"]", "");
			pw.println(str);
			pw.flush();
			pw.close();
		} catch (Exception e) {
			e.printStackTrace();
		}

		copyFile((new StringBuilder(String.valueOf(rootpath))).append("/xml/")
				.append(filename).toString(), (new StringBuilder(String
				.valueOf(rootpath))).append("/").append(target).append("/")
				.append(newfilename).toString());
	}

	public void copy3(String path, String filename, boolean rete) {
		// item_r_a_t_e_s_e_r_v_e_r.xml
		String[] fileArr = this.getUpdFileNames();
		String srcFile = fileArr[0];
		try {
			/*
			 * PrintWriter pw = new PrintWriter(new OutputStreamWriter( new
			 * FileOutputStream(request.getRealPath("/")+"/up/"+srcFile)),
			 * true);
			 */
			if (rete) {
				copyFile(request.getRealPath("/") + "/up/" + srcFile, request
						.getRealPath("/")
						+ "/" + path + "/" + filename);
			} else {
				PrintWriter pw = new PrintWriter(new OutputStreamWriter(
						new FileOutputStream(request.getRealPath("/") + "/"
								+ path + "/" + filename, false)));
				String str = readFileByLines(request.getRealPath("/") + "/up/"
						+ srcFile);
				str = str.replaceAll("[r][a][t][e][=][\"]\\d{2}[\"]", "");
				str = str.replaceAll("[r][a][t][e][=][\"]\\d{3}[\"]", "");
				str = str.replaceAll("[r][a][t][e][=][\"]\\d{4}[\"]", "");
				str = str.replaceAll("[r][a][t][e][=][\"]\\d{5}[\"]", "");
				str = str.replaceAll("[r][a][t][e][2][=][\"]\\d{2}[\"]", "");
				str = str.replaceAll("[r][a][t][e][2][=][\"]\\d{3}[\"]", "");
				str = str.replaceAll("[r][a][t][e][2][=][\"]\\d{4}[\"]", "");
				str = str.replaceAll("[r][a][t][e][2][=][\"]\\d{5}[\"]", "");
				str = str.replaceAll("[r][a][t][e][=][\"]\\d{2}[\"]", "");
				str = str.replaceAll("[r][a][t][e][=][\"]\\d{3}[\"]", "");
				str = str.replaceAll("[r][a][t][e][=][\"]\\d{4}[\"]", "");
				str = str.replaceAll("[r][a][t][e][=][\"]\\d{5}[\"]", "");
				str = str.replaceAll("[r][a][t][e][2][=][\"]\\d{2}[\"]", "");
				str = str.replaceAll("[r][a][t][e][2][=][\"]\\d{3}[\"]", "");
				str = str.replaceAll("[r][a][t][e][2][=][\"]\\d{4}[\"]", "");
				str = str.replaceAll("[r][a][t][e][2][=][\"]\\d{5}[\"]", "");
				str = str.replaceAll("[r][a][t][e][3][=][\"]\\d{2}[\"]", "");
				str = str.replaceAll("[r][a][t][e][3][=][\"]\\d{3}[\"]", "");
				str = str.replaceAll("[r][a][t][e][3][=][\"]\\d{4}[\"]", "");
				str = str.replaceAll("[r][a][t][e][3][=][\"]\\d{5}[\"]", "");
				str = str.replaceAll("[r][a][t][e][1][1][=][\"]\\d{2}[\"]", "");
				str = str.replaceAll("[r][a][t][e][1][1][=][\"]\\d{3}[\"]", "");
				str = str.replaceAll("[r][a][t][e][1][1][=][\"]\\d{4}[\"]", "");
				str = str.replaceAll("[r][a][t][e][1][1][=][\"]\\d{5}[\"]", "");
				str = str.replaceAll("[r][a][t][e][1][2][=][\"]\\d{2}[\"]", "");
				str = str.replaceAll("[r][a][t][e][1][2][=][\"]\\d{3}[\"]", "");
				str = str.replaceAll("[r][a][t][e][1][2][=][\"]\\d{4}[\"]", "");
				str = str.replaceAll("[r][a][t][e][1][2][=][\"]\\d{5}[\"]", "");

				str = str.replaceAll("[r][a][t][e][1][3][=][\"]\\d{2}[\"]", "");
				str = str.replaceAll("[r][a][t][e][1][3][=][\"]\\d{3}[\"]", "");
				str = str.replaceAll("[r][a][t][e][1][3][=][\"]\\d{4}[\"]", "");
				str = str.replaceAll("[r][a][t][e][1][3][=][\"]\\d{5}[\"]", "");

				str = str.replaceAll("[r][a][t][e][1][4][=][\"]\\d{2}[\"]", "");
				str = str.replaceAll("[r][a][t][e][1][4][=][\"]\\d{3}[\"]", "");
				str = str.replaceAll("[r][a][t][e][1][4][=][\"]\\d{4}[\"]", "");
				str = str.replaceAll("[r][a][t][e][1][4][=][\"]\\d{5}[\"]", "");
				pw.println(str);
				pw.flush();
				pw.close();
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void copyFile(String oldPath, String newPath) {
		try {
			System.out.println((new StringBuilder("oldPath:")).append(oldPath)
					.toString());
			System.out.println((new StringBuilder("newPath:")).append(newPath)
					.toString());
			int bytesum = 0;
			int byteread = 0;
			File oldfile = new File(oldPath);
			if (oldfile.exists()) {
				InputStream inStream = new FileInputStream(oldPath);
				FileOutputStream fs = new FileOutputStream(newPath);
				byte buffer[] = new byte[1444];
				while ((byteread = inStream.read(buffer)) != -1) {
					bytesum += byteread;
					fs.write(buffer, 0, byteread);
				}
				inStream.close();
				// oldfile.delete();
			}
		} catch (Exception e) {
			System.out
					.println("\u590D\u5236\u5355\u4E2A\u6587\u4EF6\u64CD\u4F5C\u51FA\u9519");
			e.printStackTrace();
		}
	}

	public String readFileByLines(String fileName) throws Exception {
		File file = new File(fileName);
		BufferedReader reader = null;
		StringBuffer sbf = new StringBuffer();
		try {
			System.out.println("以行为单位读取文件内容，一次读一整行：");
			reader = new BufferedReader(new FileReader(file));
			String tempString = null;
			int line = 1;
			// 一次读入一行，直到读入null为文件结束
			while ((tempString = reader.readLine()) != null) {
				// 显示行号
				sbf.append(tempString);
				System.out.println("line " + line + ": " + tempString);
				line++;
			}
			reader.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return sbf.toString();

	}

	private HttpServletRequest request;
	private String uploadPath;
	private static int BUFSIZE = 8192;
	private Hashtable paramHt;
	private ArrayList updFileArr;

}